package com.cy.sports.controller;


import com.cy.sports.annotation.PassToken;
import com.cy.sports.annotation.UserLoginToken;
import com.cy.sports.entity.LoginUser;
import com.cy.sports.entity.Result;
import com.cy.sports.entity.TUser;
import com.cy.sports.service.ITUserService;
import com.cy.sports.service.ITokenService;
import com.cy.sports.utils.JWTUtil;
import com.cy.sports.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器    TUser相关的为样例代码
 * </p>
 *
 * @author Garne
 * @since 2021-05-01
 */
@Api(value = "用户相关", tags = "用户管理相关的接口", description = "用户测试接口")
@RestController
@RequestMapping("/user")
public class TUserController {
    @Autowired
    ITUserService userService;
    @Autowired
    ITokenService tokenService;

    /***
     * 这个请求需要验证token才能访问
     *
     * @author: Garne
     * @date 2019/06/14
     * @return String 返回类型
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String getMessage(HttpServletRequest request) {

        // 取出token中带的用户id 进行操作
        System.out.println(JWTUtil.getUserId(request.getHeader("token")));

        return "您已通过验证";
    }

    @ApiOperation(value = "登录", notes = "登录")
    @PassToken
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<LoginUser> login(@RequestParam(value = "account", required = true) String account,
                                   @RequestParam(value = "password", required = true) String password,
                                   HttpServletResponse response) {
        TUser u = userService.login(account, password);
        LoginUser lUser = new LoginUser();
        if (null != u) {
            if (u.getStatus() == 0) {
                return ResultUtil.fail("登录失败,该用户未经过审核");
            }
            lUser.setUser(u);
            String token = tokenService.getToken(u);
            lUser.setToken(token);

            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return ResultUtil.success(lUser);
        }
        return ResultUtil.fail("登录失败");
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "新增", notes = "新建用户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addUser(@RequestBody TUser user) {
        if (null != user) {
            return userService.register(user);
        }
        return ResultUtil.fail("添加失败");
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @PassToken
    @ApiOperation(value = "注册", notes = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result registerUser(@RequestBody TUser user) {
        if (null != user) {
            return userService.register(user);
        }
        return ResultUtil.fail("添加失败");
    }

    /**
     * 删除用户
     *
     * @param targetId
     * @param userId
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除用户")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result delUser(@RequestParam(value = "targetId", required = true) int targetId,
                          @RequestParam(value = "userId", required = true) int userId) {
        return userService.delete(userId, targetId);
    }

    @ApiOperation(value = "修改密码", notes = "更新用户密码")
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    public Result updatePwd(@RequestBody TUser user) {
        return userService.updatePwd(user);
    }

    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody TUser user) {
        return userService.update(user);
    }

    /**
     * 获取用户列表
     * @return
     */
    @ApiOperation(value = "获取用户", notes = "获取用户列表 status: 0待审核，1审核通过, 不传为获取全部")
    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public Result getUsers(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                           @RequestParam(value = "status", required = false)Integer status,
                           @RequestParam(value = "role", required = false) Integer role) {
        return userService.getUsers(pageIndex, status, role);
    }
}
